package com.oa.ylms.apps.dao.leave.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.dao.leave.ILeaveCheckDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;
import com.oa.ylms.apps.entity.leave.LeaveCheckValue;
import com.oa.ylms.apps.entity.leave.LeaveInfoValue;

@SuppressWarnings("unchecked")
public class LeaveCheckDaoImpl extends HibernateDaoSupport implements
		ILeaveCheckDao {

	private List<LeaveInfoValue> list;
	private List<LeaveCheckValue> leaveCheckList;
	private int numbers;

	public List<LeaveCheckValue> queryLeaveCheckByLeaveId(final int leaveId)
			throws Exception {
		String hqlStr = "select lcv from LeaveCheckValue lcv where lcv.leaveInfo.leaveId = ?";
		final String hql = hqlStr;	
		leaveCheckList = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = 0;
				query.setInteger(index, leaveId);
				leaveCheckList = query.list();
				return leaveCheckList;
			}
		});
		return leaveCheckList;
	}

	public int add(LeaveCheckValue leaveCheck) throws Exception {
		if (leaveCheck != null) {
			this.getHibernateTemplate().save(leaveCheck);
			return leaveCheck.getCheckId();
		}
		return 0;
	}

	public int countTpagesByCheckList(final LeaveInfoValue leaveInfo,final ArchiveInfoValue archiveInfo, final int rows)
			throws Exception {
		int pageNumbers = 0;
		String hqlStr = "select liv from LeaveInfoValue liv, StaffInfoValue siv,ArchiveInfoValue aiv where liv.staffInfo.staffId = siv.staffId and siv.archiveInfo.archiveId = aiv.archiveId and liv.states = ?";
		if (archiveInfo.getStaffName() != null) {
			hqlStr += " AND aiv.staffName =? ";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = 0;
				query.setInteger(index, leaveInfo.getStates());
				if (archiveInfo.getStaffName() != null) {
					index++;
					query.setString(index, archiveInfo.getStaffName());
				}
				list = query.list();
				return list;
			}
		});
		this.numbers = list.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		}
		if (numbers == 0) {
			pageNumbers = 0;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}

	public List<LeaveInfoValue> queryCheckList(final LeaveInfoValue leaveInfo,
			final ArchiveInfoValue archiveInfo,final int rows, final int pageNo) throws Exception {
		String hqlStr = "select liv from LeaveInfoValue liv, StaffInfoValue siv,ArchiveInfoValue aiv where liv.staffInfo.staffId = siv.staffId and siv.archiveInfo.archiveId = aiv.archiveId and liv.states = ? ";
		if (archiveInfo.getStaffName() != null) {
			hqlStr += " AND aiv.staffName =? ";
		}
		final String hql = hqlStr;	
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = 0;
				query.setInteger(index, leaveInfo.getStates());
				if (archiveInfo.getStaffName() != null) {
					index++;
					query.setString(index, archiveInfo.getStaffName());
				}
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	public List<LeaveInfoValue> getList() {
		return list;
	}

	public void setList(List<LeaveInfoValue> list) {
		this.list = list;
	}

	public int getNumbers() {
		return numbers;
	}

	public void setNumbers(int numbers) {
		this.numbers = numbers;
	}

	public int getTotalNumber() throws Exception {
		
		return numbers;
	}

	public List<LeaveCheckValue> getLeaveCheckList() {
		return leaveCheckList;
	}

	public void setLeaveCheckList(List<LeaveCheckValue> leaveCheckList) {
		this.leaveCheckList = leaveCheckList;
	}

}
